const myexpress =require("express")
const commodityRouter =myexpress.Router();
const shujuku =require("./shujuku");

//商品基本信息
commodityRouter.get("/commedity",(req,res)=>{
    let sql ="select * from stonefounset";
    shujuku.query(sql,[],(err,data) =>{
        if (err){
            console.log(err);
            res.send({code:500,message:"数据库出错，请联系管理员"});
        }else {
            res.send({code:200,data:data})
        }
    })
})
//商品选择框
commodityRouter.get("/selectClassfiy",(req,res) =>{
    let sql="select * from selectclassfiy";
    shujuku.query(sql,[],(err,data) =>{
        if (err){
            console.log(err);
            res.send({code:500,message:"数据库出错，请联系管理员"});
        }else {
            res.send({code:200,data:data})
        }
    })
})

//石料删除
commodityRouter.get("/deleteProductStone",(req,res) =>{
    let stoneId =req.query.stoneId;
    let sql ="delete from stonefounset where stoneId=?";
    shujuku.query(sql,[stoneId],(err,data) =>{
        if (err){
            res.send({code:500,message:"数据库异常"});
        }else {
            if (data.affectedRows>0){
                res.send({code:200,message:"删除成功"});
            }else {
                res.send({code:201,message:"删除失败"});
            }
        }
    })
})
//石料修改
commodityRouter.post("/updateProductStone",(req,res) =>{
    let stoneId =req.body.stoneId;
    let stoneCode =req.body.stoneCode;
    let stoneName =req.body.stoneName;
    let unit =req.body.unit;
    let stoneColor =req.body.stoneColor;
    let stoneCutting =req.body.stoneCutting;
    let cuttingInstructions =req.body.cuttingInstructions;
    let sql ="update stonefounset set stoneId =?,stoneCode=?,stoneName=?,unit=?,stoneColor=?,stoneCutting=?,cuttingInstructions=?"
    shujuku.query(sql,[stoneId,stoneCode,stoneName,unit,stoneColor,stoneCutting,cuttingInstructions],(err,data) =>{
       res.send({code:200,data:data})
    });

})
//流水号位数
commodityRouter.get("/liushui",(req,res) =>{
    let sql="select * from liushui";
    shujuku.query(sql,[],(err,data) =>{
        if (err){
            console.log(err);
            res.send({code:500,message:"数据库出错，请联系管理员"});
        }else {
            res.send({code:200,data:data})
        }
    })
})
//商品基本资料设置
commodityRouter.get("/productFoundSet",(req,res) =>{
    let sql ="select * from commoditymasterdataset";
    shujuku.query(sql,[],(err,data) =>{
        if (err){
            console.log(err);
            res.send({code:500,message:"数据库出错，请联系管理员"});
        }else {
            res.send({code:200,data:data})
        }
    })
})
//商品编码表
commodityRouter.get("/productCodeSet",(req,res) =>{
    let sql ="select * from productcodetable";
    shujuku.query(sql,[],(err,data) =>{
        if (err){
            console.log(err);
            res.send({code:500,message:"数据库出错，请联系管理员"});
        }else {
            res.send({code:200,data:data})
        }
    })
})
//新增编码表
commodityRouter.post("/addProductCode",(req,res) =>{
    let stoneId =req.body.stoneId;
    let commodityCode =req.body.commodityCode;
    let commodityName =req.body.commodityName;
    let dieNo =req.body.dieNo;
    let supplier =req.body.supplier;
    let itemNumber =req.body.itemNumber;
    let tagUnitPrice =req.body.tagUnitPrice;
    let salesLaborCost =req.body.salesLaborCost;
    let laborCosPricing =req.body.laborCosPricing;
    let sql= "insert into productcodetable (stoneId,commodityCode,commodityName,dieNo,supplier,itemNumber,tagUnitPrice,salesLaborCost,laborCosPricing) values(?,?,?,?,?,?,?,?,?)";
    shujuku.query(sql,[stoneId,commodityCode,commodityName,dieNo,supplier,itemNumber,tagUnitPrice,salesLaborCost,laborCosPricing], (err,data) =>{
        if (err){
            res.send({code:500,message:"数据库异常,联系管理员"});
        }else {
            if (data.affectedRows>0){
                res.send({code:200,message:"添加成功"});
            }else {
                res.send({code:201,message:"添加失败"});
            }
        }

    })
})
//删除编码表
commodityRouter.get("/deleteProductCode",(req,res) =>{
    let stoneId =req.query.stoneId;
    let sql ="delete from productcodetable where stoneId=?";
    shujuku.query(sql,[stoneId],(err,data) =>{
        if (err){
            res.send({code:500,message:"数据库异常"});
        }else {
            if (data.affectedRows>0){
                res.send({code:200,message:"删除成功"});
            }else {
                res.send({code:201,message:"删除失败"});
            }
        }
    })
})

//批量删除编码表
commodityRouter.post("/deleteTableCode",(req,res) =>{
    let deleteId = req.body.deleteId;
     let newArr= deleteId.map(item =>{
        return item
    })
    // let stoneId =req.body.stoneId;
    let sql ="delete from productcodetable where stoneId=?";
    shujuku.query(sql,[newArr],(err,data) =>{
        if (err){
            res.send({code:500,message:"数据库异常,联系管理员"});
        }else {
            if (data.affectedRows>0){
                res.send({code:200,message:"添加成功"});
            }else {
                res.send({code:201,message:"添加失败"});
            }
        }
    })
})
//查询编码表
commodityRouter.post("/searchProductCode",(req,res) =>{
    let neirong ="%"+req.body.neirong+"%";
    let sql ="select * from productcodetable where stoneId like ?";
    shujuku.query(sql,[neirong],(err,data) =>{
        // console.log(data.data)
        res.send({code:200,data:data});
    })
})
//修改编码表
commodityRouter.post("/updateProductCode",(req,res) =>{
    let stoneId =req.body.stoneId;
    let commodityCode =req.body.commodityCode;
    let commodityName =req.body.commodityName;
    let dieNo =req.body.dieNo;
    let supplier =req.body.supplier;
    let itemNumber =req.body.itemNumber;
    let tagUnitPrice =req.body.tagUnitPrice;
    let salesLaborCost =req.body.salesLaborCost;
    let laborCosPricing =req.body.laborCosPricing;
    let sql ="update productcodetable set stoneId =?,commodityCode=?,commodityName=?,supplier=?,itemNumber=?, tagUnitPrice=?," +
        "tagUnitPrice=?,salesLaborCost=?,laborCosPricing=? where stoneId=?";
    shujuku.query(sql,[stoneId,commodityCode,commodityName,dieNo,supplier, itemNumber,tagUnitPrice,salesLaborCost,laborCosPricing],(err,data) =>{
        if (err){
            res.send({code:500,message:"数据库异常,联系管理员"});
        }else {
            if (data.affectedRows>0){
                res.send({code:200,message:"添加成功"});
            }else {
                res.send({code:201,message:"添加失败"});
            }
        }
    });

})
//批发零售
commodityRouter.get("/Wholesale",(req,res) =>{
    let sql ="select * from wholesalemanagement";
    shujuku.query(sql,[],(err,data) =>{
        if (err){
            console.log(err);
            res.send({code:500,message:"数据库出错，请联系管理员"});
        }else {
            res.send({code:200,data:data})
        }
    })
})
//批发增加工费
commodityRouter.post("/addWholesale",(req,res) =>{
    console.log(req.body)
    let lineNumber =req.body.lineNumber;
    let chengse =req.body.chengse;
    let style =req.body.style;
    let basicLaboCost =req.body.basicLaboCost;
    let otherLaboCost =req.body.otherLaboCost;
    let loss =req.body.loss;
    let microLoss =req.body.microLoss;
    let sql= "insert into wholesalemanagement (lineNumber,chengse,style,basicLaboCost,otherLaboCost,loss,microLoss) values(?,?,?,?,?,?,?)";
    shujuku.query(sql,[lineNumber,chengse,style,basicLaboCost,otherLaboCost,loss,microLoss], (err,data) =>{
        if (err){
            res.send({code:500,message:"数据库异常,联系管理员"});
        }else {
            if (data.affectedRows>0){
                res.send({code:200,message:"添加成功"});
            }else {
                res.send({code:201,message:"添加失败"});
            }
        }

    })
})


module.exports = commodityRouter;